package com.d3works.apps.talent.web;

import com.d3works.apps.talent.db.TrainingSession;
import com.d3works.apps.talent.db.repo.TrainingSessionRepository;
import com.d3works.apps.talent.service.TrainingSessionService;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Singleton;

@Singleton
public class TrainingSessionController {

  public void completeSession(ActionRequest request, ActionResponse response) {

    TrainingSession trainingSession = request.getContext().asType(TrainingSession.class);
    trainingSession = Beans.get(TrainingSessionRepository.class).find(trainingSession.getId());

    Beans.get(TrainingSessionService.class).closeSession(trainingSession);

    response.setReload(true);
  }

  public void updateAllRating(ActionRequest request, ActionResponse response) {

    TrainingSession trainingSession = request.getContext().asType(TrainingSession.class);
    trainingSession = Beans.get(TrainingSessionRepository.class).find(trainingSession.getId());

    Beans.get(TrainingSessionService.class).updateAllRating(trainingSession);

    response.setReload(true);
  }

  public void cancel(ActionRequest request, ActionResponse response) {

    TrainingSession trainingSession = request.getContext().asType(TrainingSession.class);
    trainingSession = Beans.get(TrainingSessionRepository.class).find(trainingSession.getId());

    Beans.get(TrainingSessionService.class).cancel(trainingSession);

    response.setReload(true);
  }

  public void updateTraingRegisterTraining(ActionRequest request, ActionResponse response) {

    TrainingSession trainingSession = request.getContext().asType(TrainingSession.class);

    if (trainingSession.getTrainingRegisterList() != null) {

      trainingSession =
          Beans.get(TrainingSessionService.class).updateTraingRegisterTraining(trainingSession);

      response.setValue("trainingRegisterList", trainingSession.getTrainingRegisterList());
    }
  }
}
